package com.leyou.item.controller;

import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
// TODO 这个包怎么跑到其他地址了
import com.leyou.pojo.PageResult;
import com.leyou.utils.ResponseEntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;


    /**
     * 品牌管理,分页展示
     *
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") boolean desc,
            @RequestParam(value = "key", required = false) String key
    ) {
        PageResult<Brand> brandPageResult = this.brandService.queryBrandByPageAndSort(page, rows, sortBy, desc, key);

        if (brandPageResult != null && 0 != brandPageResult.getItems().size()) {
            return ResponseEntity.ok(brandPageResult);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 新增品牌
     * @param brand
     * @param cids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        try {
            this.brandService.saveBrand(cids, brand);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }


    /**
     * 商品列表 新增/修改页面 所属品牌 数据回显
     *
     * @param cid category_id
     */
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCategory(@PathVariable("cid") Long cid) {
        List<Brand> brands = this.brandService.queryBrandByCategory(cid);
        return ResponseEntityUtils.selectResultStatus(brands);
    }


    /**
     * 品牌管理界面,根据bid删除品牌
     * @param bid  brand_id
     * @return void
     */
    @DeleteMapping("delete")
    public ResponseEntity<Void> deleteByBrandId(@RequestParam("bid") Long bid) {
        try {
            this.brandService.deleteByBrandId(bid);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.ACCEPTED).build();
        }
    }


    /**
     * search服务调用 根据bid查询品牌名称
     * @param bid
     * @return
     */
    @GetMapping("name")
    public String queryNameById(@RequestParam("bid") Long bid) {
        return this.brandService.queryNameById(bid);
    }


    /**
     * 根据bid查询brand
     *
     * @param bids brand_id List集合
     */
    @GetMapping("bids")
    public ResponseEntity<List<Brand>> queryBrandByBids(@RequestParam("bids") List<Long> bids) {
        List<Brand> brands = this.brandService.queryBrandByBids(bids);
        return ResponseEntityUtils.selectResultStatus(brands);
    }
}
